package com.wfy.common.config;

import com.google.common.collect.Maps;
import com.wfy.common.util.PropertiesLoader;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/7/1
 * Time: 22:12
 */
public class Global {

    private Global() {

    }

    public static Global getInstance() {
        return LazyHolder.GLOBAL;
    }

    private static class LazyHolder {
        private static final Global GLOBAL = new Global();
    }

    public static final String TOKEN = "token";

    public static final String YES = "true";
    public static final String NO = "false";


    public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
    public static final int DEFAULT_SUCCESS_CODE = 0;

    public static final String DEFAULT_FAIL_MSG = "FAIL";
    public static final int DEFAULT_FAIL_CODE = 1;

    /**
     * key不正确
     */
    public static final String INVALID_USER_KEY_MSG = "INVALID_USER_KEY";
    public static final int INVALID_USER_KEY_CODE = 2;

    /**
     * 参数非法
     */
    public static final String INVALID_PARAMS_MSG = "INVALID_PARAMS";
    public static final int INVALID_PARAMS_CODE = 3;

    /**
     * 缺失参数
     */
    public static final String MISSING_REQUIRED_PARAMS_MSG = "MISSING_REQUIRED_PARAMS";
    public static final int MISSING_REQUIRED_PARAMS_CODE = 4;

    /**
     * 装置不存在
     */
    public static final String NO_DEVICE_MSG = "NO_DEVICE";
    public static final int NO_DEVICE_CODE = 5;

    /**
     * 传感器不存在
     */
    public static final String NO_SENSOR_MSG = "NO_SENSOR";
    public static final int NO_SENSOR_CODE = 6;


    /**
     * 超出日访问量
     */
    public static final String DAILY_QUERY_OVER_LIMIT_MSG = "DAILY_QUERY_OVER_LIMIT";
    public static final int DAILY_QUERY_OVER_LIMIT_CODE = 11;

    /**
     * 单位时间内访问过于频繁
     */
    public static final String ACCESS_TOO_FREQUENT_MSG = "ACCESS_TOO_FREQUENT";
    public static final int ACCESS_TOO_FREQUENT_CODE = 12;


    public static final String DEFAULT_DEVICE_DATA_SPLIT = "_";
    public static final String DEFAULT_SENSOR_DATA_NAMESPACE = "smartfactory_iot_device_status";

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = Maps.newHashMap();

    /**
     * 属性文件加载对象
     */
    private static PropertiesLoader loader = new PropertiesLoader("application.properties");

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }

    /**
     * 页面获取常量
     */
    public static Object getConst(String field) {
        try {
            return Global.class.getField(field).get(null);
        } catch (Exception e) {
            // 异常代表无配置，这里什么也不做
        }
        return null;
    }

    /**
     * 获取工程路径
     *
     * @return
     */
    public static String getProjectPath() {
        // 如果配置了工程路径，则直接返回，否则自动获取。
        String projectPath = Global.getConfig("projectPath");
        if (StringUtils.isNotBlank(projectPath)) {
            return projectPath;
        }
        try {
            File file = new DefaultResourceLoader().getResource("").getFile();
            if (file != null) {
                while (true) {
                    File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
                    if (f == null || f.exists()) {
                        break;
                    }
                    if (file.getParentFile() != null) {
                        file = file.getParentFile();
                    } else {
                        break;
                    }
                }
                projectPath = file.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return projectPath;
    }

    /**
     * 获取URL后缀
     */
    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

}
